<template>
  <div class="schedules-form" v-loading="loading">
    <el-form ref="addform" :rules="rules" :model="addForm" label-width="100px" size="mini">
      <el-form-item label="分组名称" prop="groupName">
        <el-input v-model="addForm.groupName"></el-input>
      </el-form-item>
      <el-form-item label="分组代码" prop="groupCode">
        <el-input v-model="addForm.groupCode" :disabled="type==='edit'"></el-input>
      </el-form-item>
      <el-form-item size="large">
        <el-button type="primary" @click="addFun">新增</el-button>
        <el-button @click="cancelFun">取消</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>
<script>
import {
  mapGetters
} from 'vuex'
import axios from 'axios'
import {createConfig} from '@/assets/js/tools'
export default {
  name: "newsAdd",
  data() {
    return {
      loading:false,
      addForm: {
          groupCode:'',
          groupName:'',

        },
        rules:{
          groupName: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          groupCode: [
            { required: true, message: '代码不能为空', trigger: 'blur' }
          ]
        }
    };
  },
  props: ['modules','type'],
  computed: {
    ...mapGetters({
      userInfo:'getUserInfo'
    })
  },
  watch:{

  },
  methods: {
    addFun(){
      this.$refs['addform'].validate((valid) => {
       if (valid) {
         this.addModules()
       } else {
         return false;
       }
     });
    },
    addModules(){
      let param={
          groupCode:this.addForm.groupCode,
          groupName:this.addForm.groupName
      }

      this.loading=true
      let path="/station/api/meeting/group/createGroup?vs="+Date.now();
      if(this.type==='edit'){
        param.id=this.modules.id
        path="/station/api/meeting/group/editGroup?vs="+Date.now();
      }
      let config=createConfig(this.userInfo.token)
      axios.post(path, param, config).then((data) => {
        if(data.data.status==="SUCCESS"){
          this.$message({
            showClose: true,
            message: '新增分组成功',
            type: 'success'
          })
          this.$emit('hascreated');
        }else {
           this.$message({
             showClose: true,
             message: data.data.errorMsg ? data.data.errorMsg : '新增分组失败',
             type: 'error'
           })
        }
        this.loading=false
      }).catch( (err)=>{
        // eslint-disable-next-line no-console
        console.log(err)
        this.$message({
          showClose: true,
          message: '新增模块失败',
          type: 'error'
        })
        this.loading=false
      })
    },
    cancelFun(){
      this.$emit('cancelAdd');
    }
  },
  mounted(){
    //this.addForm.endDate=this.date
    if(this.type==='edit'){
      this.addForm=Object.assign({}, this.modules);
      
    }

  }
};
</script>
<style scoped lang="scss">
  .schedules-form{
    position: relative;
  }
</style>
